# 字典 不允许元素重复
# 字典字面量
{"王力宏": 99,
 "周杰伦": 88,
 "林俊杰": 77,
 }
# 变量字典
my_dict = {"王力宏": 99,
           "周杰伦": 88,
           "林俊杰": 77,
           }
# 空字典
my_dict = {}
my_dict = dict()
# 字典不允许相同的key值出现，如果写了只会保留后一个，覆盖
my_dict = {"王力宏": 99,
           "王力宏": 88,
           "王力宏": 77,
           }
print(my_dict)
# 获取value值
my_dict = {"王力宏": 99,
           "周杰伦": 88,
           "林俊杰": 77,
           }
print("王力宏成绩是", my_dict["王力宏"])
print("周杰伦成绩是", my_dict["周杰伦"])
print("林俊杰成绩是", my_dict["林俊杰"])
# 字典嵌套 key和value可以是任意类型 但是key不可以是字典
my_dict = {
    "王力宏": {
        "语文": 77,
        "数学": 66,
        "英语": 33
    },
    "周杰伦": {
        "语文": 88,
        "数学": 86,
        "英语": 55
    },
    "林俊杰": {
        "语文": 99,
        "数学": 96,
        "英语": 66
    }

}
# 打印全部
print(my_dict)
# 获取王力宏的信息
print(my_dict["王力宏"])
# 获取王力宏的语文信息
print(my_dict["王力宏"]["语文"])
# 字典常用操作
my_dict = {"王力宏": 99,
           "周杰伦": 88,
           "林俊杰": 77,
           }
#新增数据，修改数据
my_dict["王勇"] = 99
print("新增后",my_dict)
my_dict["王勇"] = 89
print("修改后",my_dict)
#删除元素.pop(key)
my_dict = {"王力宏": 99,
           "周杰伦": 88,
           "林俊杰": 77,
           }
my_dict.pop('王力宏')
print("删除王力宏后",my_dict)
#清空元素
my_dict.clear()
print("清空后",my_dict)
#获取全部的.keys（）
my_dict = {"王力宏": 99,
           "周杰伦": 88,
           "林俊杰": 77,
           }
keys=my_dict.keys()
print("全部的key：",keys)
#遍历字典
#1获取全部的key遍历
print("for遍历keys")
for x in keys:
    print(x+":",my_dict[x])
#2直接for遍历 获取key
print("直接for遍历字典")
for x in my_dict:
    print(x+":",my_dict[x])
#统计元素
print("字典元素个数",len(my_dict))